package com.toefl.speaking.util;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnKeyListener;
import android.os.AsyncTask;
import android.view.KeyEvent;
import android.widget.Button;

import com.ihandy.core.Constants;
import com.toefl.speaking.activity.base.StepListActivity;
import com.toefl.speaking.helper.DialogHelper;

/**
 * 带进度条的AsyncTask
 * @author over1
 * @param <Params>
 * @param <Progress>
 * @param <Result>
 */
public abstract class ProgressAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
	private static final String TAG = "ProgressAsyncTask";
	protected ProgressDialog mDialog;
	/** 对话框标题 */
	private String mTitle;
	private Context ctx;
	private String mMessage;

	/** 是否禁用进度条 "取消按钮" */
	private boolean disableCancel = true;

	/**
	 * 构造函数
	 * @param pTitle 对话框标题
	 */
	public ProgressAsyncTask(Context ctx, String pTitle) {
		this.mTitle = pTitle;
		this.ctx = ctx;
		this.mMessage = Constants.LOADING;
	}

	public ProgressAsyncTask(Context ctx, String pTitle, boolean disableCancel) {
		this.mTitle = pTitle;
		this.ctx = ctx;
		this.mMessage = Constants.LOADING;
		this.disableCancel = disableCancel;
	}

	public ProgressAsyncTask(Context ctx, String title, String message) {
		this.mTitle = title;
		this.mMessage = message;
		this.ctx = ctx;
	}

	/**
	 * 显示正在处理
	 */
	@Override
	protected void onPreExecute() {
		if (mDialog == null) {
			mDialog = DialogHelper.getProgress(this.ctx, mTitle);
			mDialog.setMessage(this.mMessage);
			mDialog.setCancelable(false); // guangbing
			mDialog.setOnKeyListener(new OnKeyListener() {

				@Override
				public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
					if (keyCode == KeyEvent.FLAG_KEEP_TOUCH_MODE) {
						proTaskShow();
					}
					if (keyCode == KeyEvent.KEYCODE_BACK) { // guangbing
						cancel(true);
						proTaskShow();
						mDialog.dismiss();
					}
					return false;
				}
			});
			mDialog.setOnCancelListener(new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					cancel(true); // 如果Task还在运行，则先取消它
					proTaskShow();
				}
			});
		}
		if (!mDialog.isShowing()) {
			mDialog.show();

			// modify 2011-9-3 tj 进度条不能取消
			if (disableCancel) {
				Button cancel = mDialog.getButton(-4);// ProgressDialog.BUTTON_CANCEL
				if (cancel != null) {
					cancel.setEnabled(false);
				}
			}
		}
	}

	@Override
	protected void onPostExecute(Result result) {
		if (mDialog.isShowing()) {
			mDialog.dismiss();
		}
	}

	/** 更新进度条的 消息 */
	public void updateProgressMsg(Progress msg) {
		publishProgress(msg);
	}

	/** 设置异步处理task为null **/
	private void proTaskShow() {
		// if(ctx instanceof StepFormActivity){
		// ((StepFormActivity) ctx).proTask=null;
		// }else if(ctx instanceof StepListActivity) {
		// ((StepListActivity) ctx).proTask=null;
		// }
	}
}
